// SettingsPage.cpp

#include "StdAfx.h"

#include "Common/String/StringConvert.h"

#ifndef UNDER_CE
#include "Common/System/MemoryLock.h"
#endif

#include "HelpUtils.h"
#include "Common/Lang/LangUtils.h"
#include "Common/App/ProgramLocation.h"
#include "Common/Registry/RegistryUtils.h"
#include "SettingsPage.h"

#include "resource.h"

using namespace Common;
using namespace Common::System;

static Lang::CIDLangPair kIDLangPairs[] =
{
  { IDC_SETTINGS_SHOW_DOTS, 0x03010401},
  { IDC_SETTINGS_SHOW_REAL_FILE_ICONS, 0x03010402},
  { IDC_SETTINGS_SHOW_SYSTEM_MENU, 0x03010410},
  { IDC_SETTINGS_FULL_ROW, 0x03010420},
  { IDC_SETTINGS_SHOW_GRID, 0x03010421},
  { IDC_SETTINGS_SINGLE_CLICK, 0x03010422},
  // { IDC_SETTINGS_UNDERLINE, 0x03010423}
  { IDC_SETTINGS_ALTERNATIVE_SELECTION, 0x03010430},
  { IDC_SETTINGS_LARGE_PAGES, 0x03010440}
};

static LPCTSTR kEditTopic = _T("FM/options.htm#settings");

extern bool IsLargePageSupported();

LRESULT CSettingsPage::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
  Lang::LangSetDlgItemsText(HWND(*this), kIDLangPairs, sizeof(kIDLangPairs) / sizeof(kIDLangPairs[0]));

  CButton wnd;
  wnd = GetDlgItem(IDC_SETTINGS_SHOW_DOTS);
  wnd.SetCheck(Registry::ReadShowDots());

  
  wnd = GetDlgItem(IDC_SETTINGS_SHOW_SYSTEM_MENU);
  wnd.SetCheck(Registry::ReadShowSystemMenu());

  wnd = GetDlgItem(IDC_SETTINGS_SHOW_REAL_FILE_ICONS);
  wnd.SetCheck(Registry::ReadShowRealFileIcons());

  wnd = GetDlgItem(IDC_SETTINGS_FULL_ROW);
  wnd.SetCheck(Registry::ReadFullRow());

  wnd = GetDlgItem(IDC_SETTINGS_SHOW_GRID);
  wnd.SetCheck(Registry::ReadShowGrid());

  wnd = GetDlgItem(IDC_SETTINGS_ALTERNATIVE_SELECTION);
  wnd.SetCheck(Registry::ReadAlternativeSelection());

  wnd = GetDlgItem(IDC_SETTINGS_LARGE_PAGES);
  
  if (IsLargePageSupported())
    wnd.SetCheck(Registry::ReadLockMemoryEnable());
  else
    wnd.EnableWindow(FALSE);
  
  wnd = GetDlgItem(IDC_SETTINGS_SINGLE_CLICK);
  wnd.SetCheck(Registry::ReadSingleClick());


  return TRUE;
}

int CSettingsPage::OnApply()
{
  Registry::SaveShowDots(IsDlgButtonChecked(IDC_SETTINGS_SHOW_DOTS));
  Registry::SaveShowSystemMenu(IsDlgButtonChecked(IDC_SETTINGS_SHOW_SYSTEM_MENU));
  Registry::SaveShowRealFileIcons(IsDlgButtonChecked(IDC_SETTINGS_SHOW_REAL_FILE_ICONS));

  Registry::SaveFullRow(IsDlgButtonChecked(IDC_SETTINGS_FULL_ROW));
  Registry::SaveShowGrid(IsDlgButtonChecked(IDC_SETTINGS_SHOW_GRID));
  Registry::SaveAlternativeSelection(IsDlgButtonChecked(IDC_SETTINGS_ALTERNATIVE_SELECTION));
  #ifndef UNDER_CE
  if (IsLargePageSupported())
  {
    BOOL enable = IsDlgButtonChecked(IDC_SETTINGS_LARGE_PAGES);
    Security::EnableLockMemoryPrivilege(enable);
    Registry::SaveLockMemoryEnable(enable);
  }
  #endif
  
  Registry::SaveSingleClick(IsDlgButtonChecked(IDC_SETTINGS_SINGLE_CLICK));
  return TRUE;
}

void CSettingsPage::OnHelp()
{
  ShowHelpWindow(NULL, kEditTopic); // change it
}
